package com.SouthernWall_404.AgeRight.Common.API.Capability;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.common.util.INBTSerializable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class Locks implements INBTSerializable<CompoundTag> {



    public List<BlockPos> lockPoses=new ArrayList<>();
    public List<UUID> lockUUID=new ArrayList<>();

    public static String SIZE_TAG="size";
    public static String UUID_TAG="uuid_";
    public static String X_TAG="x_";
    public static String Y_TAG="y_";
    public static String Z_TAG="z_";

    public static String UUIDLIST_LAG="uuidpack";
    public static String POSLIST_TAG="postlist_tag";

    @Override
    public CompoundTag serializeNBT() {

        CompoundTag nbt=new CompoundTag();

        CompoundTag listPack=getUUIDPack();
        CompoundTag posPack=getPosPack();

        nbt.put(UUIDLIST_LAG,listPack);
        nbt.put(POSLIST_TAG,posPack);

        return nbt;
    }
    @Override
    public void deserializeNBT(CompoundTag nbt) {
        lockPoses=new ArrayList<>();
        lockUUID=new ArrayList<>();
        loadPosPack(nbt);
        loadUUIDPack(nbt);
    }
    public CompoundTag getPosPack()
    {
        CompoundTag posPack=new CompoundTag();

        int size=this.lockPoses.size();

        posPack.putInt(SIZE_TAG,size);

        for(int i=0;i<size;i++)
        {
            String X=X_TAG+i;
            String Y=Y_TAG+i;
            String Z=Z_TAG+i;
            BlockPos blockPos=lockPoses.get(i);
            posPack.putInt(X,blockPos.getX());
            posPack.putInt(Y,blockPos.getY());
            posPack.putInt(Z,blockPos.getZ());
        }

        return posPack;
    }
    public void loadPosPack(CompoundTag nbt)
    {
        if(nbt.contains(POSLIST_TAG))
        {
            CompoundTag posPack=nbt.getCompound(POSLIST_TAG);

            if(posPack.contains(SIZE_TAG))
            {
                int size=posPack.getInt(SIZE_TAG);
                for(int i=0;i<size;i++)
                {
                    String X=X_TAG+i;
                    String Y=Y_TAG+i;
                    String Z=Z_TAG+i;
                    if(posPack.contains(X)&&posPack.contains(Y)&&posPack.contains(Z))
                    {

                        int x=posPack.getInt(X);
                        int y=posPack.getInt(Y);
                        int z=posPack.getInt(Z);

                        BlockPos blockPos=new BlockPos(x,y,z);
                        lockPoses.add(blockPos);
                    }

                }

            }


        }

    }
    public CompoundTag getUUIDPack()
    {
        CompoundTag listPack=new CompoundTag();

        int size=this.lockUUID.size();

        listPack.putInt(SIZE_TAG,size);

        for(int i=0;i<size;i++)
        {
            String TAG=UUID_TAG+i;
            listPack.putUUID(TAG,lockUUID.get(i));

        }

        return listPack;
    }
    public void loadUUIDPack(CompoundTag nbt)
    {
        if(nbt.contains(UUIDLIST_LAG))
        {
            CompoundTag listPack=nbt.getCompound(UUIDLIST_LAG);

            if(listPack.contains(SIZE_TAG))
            {
                int size=listPack.getInt(SIZE_TAG);
                for(int i=0;i<size;i++)
                {
                    String tag=UUID_TAG+i;

                    if(listPack.contains(tag))
                    {
                        UUID uuid=listPack.getUUID(tag);
                        lockUUID.add(uuid);
                    }
                }
            }
        }
    }

    public BlockPos getPos(UUID uuid)
    {
        int i=lockUUID.indexOf(uuid);
        return lockPoses.get(i);
    }

    public UUID getUUID(BlockPos blockPos)
    {
        int i=lockPoses.indexOf(blockPos);
        return lockUUID.get(i);
    }
}
